<template>
  <div>
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>确认要完成盘点号为（{{ formItem.code }}）的盘点计划吗？该盘点计划下包含（{{ formItem.dutyItems.length }}）条盘点任务! </span>
      </div>
      <div>
        <span v-if="unFinish !== 0" style="color: red">注意：盘点任务中还有（{{ unFinish }} ）条盘点任务未完成</span>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  detail,
  donePlan
} from '@neu/dcam-ui/api/inventoryPlan'
export default {
  name: 'InventoryPlanDoneForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    return {
      formItem: {
        name: '',
        code: '',
        dutyItems: []
      },
      unFinish: 0
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: {
      handler(newVal, oldVal) {
        this.fetch()
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
          const data = this.formItem.dutyItems.filter(l => l.status !== 'finish')
          this.unFinish = data.length
        })
      }
    },
    /** 盘点计划完成 */
    onOk() {
      return new Promise(resolve => {
        resolve(donePlan(this.id))
      })
    }
  }
}
</script>
